package org.asm.common.delay;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.AdviceAdapter;

/**
 * @description:
 * @author: LiuLei
 * @create: 2024-09-30
 **/
public class DelayAdviceAdapter extends AdviceAdapter {
    protected DelayAdviceAdapter(MethodVisitor mv, int access, String name, String descriptor) {
        super(ASM7, mv, access, name, descriptor);
    }
    @Override
    protected void onMethodEnter() {
        // 在方法开始处插入10秒延迟
        mv.visitLdcInsn(10000L); // 加载常量10000L 所有的方案执行，都是参数先入栈
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "sleep", "(J)V", false); // 调用Thread.sleep(long) // INVOKESTATIC 这个为静态方法


        // todo: 这块待研究
        // try-catch块：捕获并处理InterruptedException
        mv.visitLabel(new org.objectweb.asm.Label());
        mv.visitTryCatchBlock(
                new org.objectweb.asm.Label(), // try开始
                new org.objectweb.asm.Label(), // try结束
                new org.objectweb.asm.Label(), // catch开始
                "java/lang/InterruptedException" // 捕获的异常类型
        );
        mv.visitInsn(RETURN); // 捕获到异常后返回

        super.onMethodEnter(); // 在方法其余部分执行正常代码
    }
}